// src/api/auth.ts
import  http  from './http';
import {TokenResponse} from './types'
interface LoginParams {
  username: string;
  password: string;
}



interface RegisterParams {
  phone: string;
  username: string;
  password: string;
  birthDate: string;
}

interface SendCodeParams {
  phone: string;
}


interface SmsLoginParams {
    phone: string;
    code: string;
}


// interface TokenResponse {
//   code: number;
//   data: {
//     accessToken: string;
//     refreshToken: string;
//     expiresIn: number;
//   };
// }

export const authApi = {

  // 短信验证码登录
  smsLogin: (data: SmsLoginParams):Promise<TokenResponse> => http.post('/sms/login', data),

  // 密码登录
  login: (data: LoginParams):Promise<TokenResponse> => http.post('/login', data),
  
  // 手机号注册
  register: (data: RegisterParams):Promise<TokenResponse> => http.post('/register', data),
  
  // 发送验证码
  sendVerificationCode: (data: SendCodeParams):Promise<TokenResponse> => http.post('/send', data),
  
  // 刷新Token
  refreshToken: (refreshToken: string):Promise<TokenResponse> => http.post('/refresh', { refreshToken }),
  
  // 获取用户信息
  getProfile: () => http.get('/profile'),
};


// src/api/auth.ts (部分代码)
export const refreshToken = async (refreshToken: string) => {
    try {
      const response = await http.post('/refresh', { refreshToken });
      return response;
    } catch (error) {
      throw new Error('Token刷新失败');
    }
  };